package com.eternal.base.protocol;

import com.eternal.base.concat.DeviceMinModel;
import com.eternal.base.concat.DeviceModel;
import com.eternal.base.global.BluetoothKey;
import com.eternal.base.utils.ByteUtils;
import com.eternal.framework.utils.KLog;

public class ModelProtocolResolution extends ProtocolResolution {
    public static byte[] getCModelAlarmData(int sequence) {
        return addHead(new byte[]{BluetoothKey.DEVICE_CURRENT_TEMP_HUM_STATUS,
                        BluetoothKey.DEVICE_TMP_UNIT, BluetoothKey.DEVICE_C_MODEL_ALARM},
                BluetoothKey.COMMAND_GET, sequence);
    }

    public static DeviceMinModel parseCMinModel(byte[] packet) {
        packet = check(packet);
        DeviceMinModel model = new DeviceMinModel();
        if (packet[0] == BluetoothKey.DEVICE_CURRENT_TEMP_HUM_STATUS) {
            model.tmpState = (byte) ByteUtils.getBits(packet[2], 4, 2);
            model.humState = (byte) ByteUtils.getBits(packet[2], 6, 2);
            model.tmp = ByteUtils.getShort(packet, 3);
            model.per = ByteUtils.getShort(packet, 5);
        } else {
            KLog.e("model err:" + ByteUtils.bytes2HexString(packet));
            return model;
        }

        if (packet[7] == BluetoothKey.DEVICE_TMP_UNIT) {
            model.isDegree = !ByteUtils.getBit(packet[9], 7);
        } else {
            KLog.e("model err:" + ByteUtils.bytes2HexString(packet));
            return model;
        }
        if (packet[10] == BluetoothKey.DEVICE_C_MODEL_ALARM) {
            System.arraycopy(packet, 12, model.values, 0, 7);
        } else {
            KLog.e("model err:" + ByteUtils.bytes2HexString(packet));
        }
        return model;
    }

    public static byte[] setCModelData(DeviceModel model, int sequence) {
        byte[] packet;
        if (model.isDegree) {
            packet = new byte[]{0x19, 0x7, ProtocolTransformer.getTmpSwitch(model),
                    (byte) ProtocolTransformer.getFah(model.autoHighTmp), model.autoHighTmp,
                    (byte) ProtocolTransformer.getFah(model.autoLowTmp), model.autoLowTmp,
                    model.autoHighHum, model.autoLowHum};
        } else {
            packet = new byte[]{0x19, 0x7, ProtocolTransformer.getTmpSwitch(model),
                    model.autoHighTmp, (byte) ProtocolTransformer.getDegree(model.autoHighTmp),
                    model.autoLowTmp, (byte) ProtocolTransformer.getDegree(model.autoLowTmp),
                    model.autoHighHum, model.autoLowHum};
        }
        return addHead(packet, BluetoothKey.COMMAND_SET, sequence);
    }
}
